/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CU009.java
 *
 * Created on 03/11/2012, 12:53:05
 */
package victory;

import baseDeDatos.ConexionMySQL;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author Enrique
 */
public class CU009 extends javax.swing.JPanel {

    /** Creates new form CU009 */
    public CU009() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        boton_cancelar = new javax.swing.JButton();
        guardar = new javax.swing.JButton();
        boton_cargarImagen = new javax.swing.JButton();
        label_imagenParticipante = new javax.swing.JLabel();
        jp_imagenParticipante = new javax.swing.JLayeredPane();
        correoElectronico = new javax.swing.JTextField();
        nombreParticipante = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        label_nombreParticipante = new javax.swing.JLabel();
        label_correoElectronico = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(victory.VictoryApp.class).getContext().getResourceMap(CU009.class);
        boton_cancelar.setText(resourceMap.getString("boton_cancelar.text")); // NOI18N
        boton_cancelar.setName("boton_cancelar"); // NOI18N

        guardar.setText(resourceMap.getString("guardar.text")); // NOI18N
        guardar.setName("guardar"); // NOI18N
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarActionPerformed(evt);
            }
        });

        boton_cargarImagen.setText(resourceMap.getString("boton_cargarImagen.text")); // NOI18N
        boton_cargarImagen.setName("boton_cargarImagen"); // NOI18N
        boton_cargarImagen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_cargarImagenActionPerformed(evt);
            }
        });

        label_imagenParticipante.setText(resourceMap.getString("label_imagenParticipante.text")); // NOI18N
        label_imagenParticipante.setName("label_imagenParticipante"); // NOI18N

        jp_imagenParticipante.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jp_imagenParticipante.setName("jp_imagenParticipante"); // NOI18N

        correoElectronico.setName("correoElectronico"); // NOI18N

        nombreParticipante.setName("nombreParticipante"); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 2, 9)); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        label_nombreParticipante.setText(resourceMap.getString("label_nombreParticipante.text")); // NOI18N
        label_nombreParticipante.setName("label_nombreParticipante"); // NOI18N

        label_correoElectronico.setText(resourceMap.getString("label_correoElectronico.text")); // NOI18N
        label_correoElectronico.setName("label_correoElectronico"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(139, 139, 139)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label_imagenParticipante)
                    .addComponent(boton_cargarImagen)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_correoElectronico)
                    .addComponent(jLabel5)
                    .addComponent(label_nombreParticipante))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jp_imagenParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                        .addComponent(guardar)
                        .addGap(18, 18, 18)
                        .addComponent(boton_cancelar))
                    .addComponent(correoElectronico, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                    .addComponent(nombreParticipante, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE))
                .addGap(217, 217, 217))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jLabel5)
                .addGap(63, 63, 63)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_nombreParticipante))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(correoElectronico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_correoElectronico, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jp_imagenParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label_imagenParticipante)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(boton_cargarImagen))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(guardar)
                        .addComponent(boton_cancelar)))
                .addContainerGap(325, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarActionPerformed
       
//TODO ver tootooooo guardarActionPerformed:        
       if(validar())
       {
           JOptionPane.showMessageDialog(null, "Los datos se han guardado satisfactoriamente");
       }
       else JOptionPane.showMessageDialog(null, "no se han podido cargar los datos");
        
        
        ConexionMySQL mysql = new ConexionMySQL();
       Connection conector = mysql.Conectar();
       
       String nom, correo;
        String sSQL = "";
        int var = 1;
        nom= nombreParticipante.getText();
        correo= correoElectronico.getText();
        
         sSQL = "INSERT INTO participante(CorreoElectronico, eliminado, nombre)" +
                "VALUES(?, ?, ?)";
          
        try {
            PreparedStatement pst = conector.prepareStatement(sSQL);
            pst.setString(1, correo);
            pst.setBoolean(2, true );
            pst.setString(3, nom);
            
            int n = pst.executeUpdate();

            if(n > 0)
            {
                JOptionPane.showMessageDialog(null, "se guardo todo bichi");
              
            }
            
            
        } catch (SQLException ex) {
           JOptionPane.showConfirmDialog(null, ex);
        }
         
    }//GEN-LAST:event_guardarActionPerformed
//TODO ver tootooooo boton_cargarImagen:
    private void boton_cargarImagenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_cargarImagenActionPerformed
Runtime r = Runtime.getRuntime();
		Process p = null;

		try {
			p = r.exec("explorer.exe");
		} catch (Exception e) {
			System.out.println("Error al ejecutar");
		}        // TODO add your handling code here:
    }//GEN-LAST:event_boton_cargarImagenActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton_cancelar;
    private javax.swing.JButton boton_cargarImagen;
    private javax.swing.JTextField correoElectronico;
    private javax.swing.JButton guardar;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLayeredPane jp_imagenParticipante;
    private javax.swing.JLabel label_correoElectronico;
    private javax.swing.JLabel label_imagenParticipante;
    private javax.swing.JLabel label_nombreParticipante;
    private javax.swing.JTextField nombreParticipante;
    // End of variables declaration//GEN-END:variables
//TODO ver tootooooo metodo Validar:
    private boolean validar() {
        String mensaje="Verifique los siguientes datos:\n";
        if( nombreParticipante.getText().toUpperCase().matches( "[A-Z][A-ZA-Z]*" ) && correoElectronico.getText().matches("^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$"))
        {
         return true;   
        }
        if(!nombreParticipante.getText().toUpperCase().matches( "[A-Z][A-ZA-Z]*" ))
            mensaje+="Nombre del participante\n";
        if(!correoElectronico.getText().matches("^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$"))
            mensaje+="Correo Electrónico";
            JOptionPane.showMessageDialog(null, mensaje);
        return false;
    }
}
